if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready',plusReady,false);
}
function plusReady () {
	
	var userInfo = getData("user_data");
	
	$(document).ready(function() {
		UpdateState();
		if(userInfo == null || userInfo.type != "success"){
			plus.nativeUI.alert("用户没有登录");
			window.location.href='login.html';
		}
		//初始化用户名
		$("#username").append(userInfo.msg.cname);
		
		data_init();
	});
	
	function data_init () {
		var log_list = showLogList(userInfo.msg.id);
//		console.log(log_list);
		var list_group = $(".list-group");
		list_group.empty();
		removeItem("item");
		for (var i=log_list.length-1; i>=0; i--) {
			var data = getData(log_list[i]);
			var html = '<div class="row col-md-6 col-xs-6 col-sm-6" style="padding-bottom: 30px;">'+
							'<a href="flight_log_detail.html" class="list-group-item col-md-10 col-xs-10 col-sm-10" data-item='+log_list[i]+' data-uploaded='+(data.uploaded==null?"":data.uploaded)+'>'+
								'<div class="panel panel-'+(data.uploaded==true?"default":"success")+'">'+
									'<div class="panel-heading">'+
										'<label>'+
											'<input type="checkbox" name="log_list" />&nbsp;&nbsp;日期：'+data.flight_date+
										'</label>'+
									'</div>'+
									'<table class="table">'+
										'<thead>'+
											'<tr>'+
												'<th>序号</th>'+
												'<th>航班号</th>'+
												'<th>航线</th>'+
												'<th>是否延误</th>'+
											'</tr>'+
										'</thead>'+
										'<tbody>'+
											'<tr>'+
												'<td>'+log_list[i].replace("LogList"+userInfo.msg.id,"")+'</td>'+
												'<td>AQ'+data.flight_com+'</td>'+
												'<td>'+data.flight_voyage+'</td>'+
												'<td>'+(data.flight_instance=="1"?"正常":"延误")+'</td>'+
											'</tr>'+
										'</tbody>'+
									'</table>'+
								'</div>'+
							'</a>'+
						'</div>';
			list_group.append(html);	
		}
		
		//隐藏选择按钮
		$(".panel-default").find("input").hide();
		$(".list-group-item").on("click", function(){
			var item = $(this).attr("data-item");
			setData("item",item);
		});
	}
	
	
	//新建
	$("#new_log").on("click",function () {
		window.location.href = "flight_log_detail.html"
	});
	
	//删除
	$("#delete_log").on("click",function () {
		    if(($(".panel-default").length + $("input[name='log_list']:checked").length) > 0){
		    	plus.nativeUI.confirm("确认删除吗？", function (event) {
				if (0 == event.index) {
					$(".panel-default").each(function () {
						var data_item = $(this).parent();
						var div_col_4 = data_item.parent();
						removeItem(data_item.attr("data-item"))
						div_col_4.remove();
					});
					$("input[name='log_list']:checked").each(function () {
						var data_item = $(this).parent().parent().parent().parent();
						var div_col_4 = data_item.parent();
						removeItem(data_item.attr("data-item"))
						div_col_4.remove();
					});
					removeItem("item");
					plus.nativeUI.alert("删除成功！");
				}
			});
		   }else{
		   	   plus.nativeUI.alert("您尚未选中任何日志信息！");
		   }
	});
	
	
	//上传
	$("#upload_log").on("click",function () {
		UpdateState();
		if (!isOnLine) {
			plus.nativeUI.alert("请在联网的情况下上传乘务日志！");
			return;
		}
		var url = server_url+'/9airweb/addlog.action';
		console.log(url);
		var checked = $("input[name='log_list']:checked");
		if (checked.length == 0) {
			plus.nativeUI.alert("您尚未选中任何日志信息！");
		} else{
			plus.nativeUI.confirm("确认上传吗？", function (event) {
				if (0 == event.index) {
					$("input[name='log_list']:checked").each(function () {
						var panel = $(this).parent().parent().parent();
						var item = panel.parent().attr("data-item");
						var data = getData(item);
						
						$.ajax({
								type:"post",
							    url: url,
							    data: data_to_require (data),
							    timeout:15000
						}).done(function(backdata, status){
							if (status=='success' && backdata.type == "success") {
								setUploaded(item);
								panel.removeClass("panel-success").addClass("panel-default");
								//隐藏选择按钮
								$(".panel-default").find("input").attr("checked", false); //2015-04-22修改————修复重复提交
								$(".panel-default").find("input").hide();
								$(".panel-default").find("input").attr("disabled",true);
								plus.nativeUI.alert("上传成功！");
							}else if(status=='success' && backdata.type == "error"){
								setUploaded(item);
								panel.removeClass("panel-success").addClass("panel-default");
								//隐藏选择按钮
								$(".panel-default").find("input").attr("checked", false); //2015-04-22修改————修复重复提交
								$(".panel-default").find("input").hide();
								$(".panel-default").find("input").attr("disabled",true);
								plus.nativeUI.alert(backdata.msg);
							}
						}).fail(function(jqXHR, textStatus){
						    if(textStatus == 'timeout') {
						        plus.nativeUI.alert("服务器异常，上传失败！");
						    }
						});
					});
					removeItem("item");
				}
			});
		}
	});
	
	function data_to_require (data) {
		var array = [];
		var require = "";
		$.each(data,function (key , value) {
			if (value!=null && key != "A1_list" && key != "safe" &&  key != "MPS_list" && key != "SC_list" && key != "ST_list" && key != "FP_list") {
				array[array.length] = "log."+key+"="+value;
			}
		});
		for (var i = 0; i<array.length; i++) {
			require += array[i]+"&";
		}
		require = require.substring(0,require.length-1);
		return require;
	}
}